Proper connect_port
[juce-lv2.git] / juce / source / extras / the jucer / src / model / components / jucer_TreeViewHandler.h
blob3ed583f0253acf6a8bba17939795659890652b37
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_TREEVIEWHANDLER_JUCEHEADER__
27 #define __JUCER_TREEVIEWHANDLER_JUCEHEADER__
30 //==============================================================================
31 /**
33 class TreeViewHandler : public ComponentTypeHandler
35 public:
36 //==============================================================================
37 TreeViewHandler()
38 : ComponentTypeHandler ("TreeView", "TreeView", typeid (DemoTreeView), 150, 150)
40 registerColour (TreeView::backgroundColourId, "background", "backgroundColour");
41 registerColour (TreeView::linesColourId, "lines", "linecol");
44 //==============================================================================
45 Component* createNewComponent (JucerDocument*)
47 return new DemoTreeView();
50 XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
52 TreeView* const t = dynamic_cast <TreeView*> (comp);
53 XmlElement* const e = ComponentTypeHandler::createXmlFor (comp, layout);
55 e->setAttribute ("rootVisible", t->isRootItemVisible());
56 e->setAttribute ("openByDefault", t->areItemsOpenByDefault());
58 return e;
61 bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
63 if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
64 return false;
66 TreeView defaultTreeView;
67 TreeView* const t = dynamic_cast <TreeView*> (comp);
69 t->setRootItemVisible (xml.getBoolAttribute ("rootVisible", defaultTreeView.isRootItemVisible()));
70 t->setDefaultOpenness (xml.getBoolAttribute ("openByDefault", defaultTreeView.areItemsOpenByDefault()));
72 return true;
75 void getEditableProperties (Component* component, JucerDocument& document, Array <PropertyComponent*>& properties)
77 ComponentTypeHandler::getEditableProperties (component, document, properties);
78 TreeView* const t = dynamic_cast <TreeView*> (component);
80 properties.add (new TreeViewRootItemProperty (t, document));
81 properties.add (new TreeViewRootOpennessProperty (t, document));
83 addColourProperties (t, document, properties);
86 const String getCreationParameters (Component* comp)
88 return quotedString (comp->getName());
91 void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
93 TreeView defaultTreeView;
94 TreeView* const t = dynamic_cast <TreeView*> (component);
96 ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
98 if (defaultTreeView.isRootItemVisible() != t->isRootItemVisible())
100 code.constructorCode
101 << memberVariableName << "->setRootItemVisible ("
102 << boolToString (t->isRootItemVisible()) << ");\n";
105 if (defaultTreeView.areItemsOpenByDefault() != t->areItemsOpenByDefault())
107 code.constructorCode
108 << memberVariableName << "->setDefaultOpenness ("
109 << boolToString (t->areItemsOpenByDefault()) << ");\n";
112 code.constructorCode << getColourIntialisationCode (component, memberVariableName);
114 code.constructorCode << "\n";
117 private:
118 //==============================================================================
119 class DemoTreeView : public TreeView
121 public:
122 DemoTreeView()
123 : TreeView ("new treeview")
125 setRootItem (new DemoTreeViewItem ("Demo root node", 4));
128 ~DemoTreeView()
130 TreeViewItem* root = getRootItem();
131 setRootItem (0);
132 delete root;
135 private:
136 class DemoTreeViewItem : public TreeViewItem
138 public:
139 DemoTreeViewItem (const String& name_, const int numItems)
140 : name (name_)
142 for (int i = 0; i < numItems; ++i)
143 addSubItem (new DemoTreeViewItem ("Demo sub-node " + String (i), numItems - 1));
146 ~DemoTreeViewItem()
150 void paintItem (Graphics& g, int width, int height)
152 if (isSelected())
153 g.fillAll (Colours::lightblue);
155 g.setColour (Colours::black);
156 g.setFont (height * 0.7f);
157 g.drawText (name, 4, 0, width - 4, height, Justification::centredLeft, true);
160 bool mightContainSubItems()
162 return true;
165 const String name;
169 //==============================================================================
170 class TreeViewRootItemProperty : public ComponentBooleanProperty <TreeView>
172 public:
173 TreeViewRootItemProperty (TreeView* comp, JucerDocument& document)
174 : ComponentBooleanProperty <TreeView> ("show root item", "Root item visible", "Root item visible", comp, document)
178 void setState (bool newState)
180 document.perform (new TreeviewRootChangeAction (component, *document.getComponentLayout(), newState),
181 "Change TreeView root item");
184 bool getState() const
186 return component->isRootItemVisible();
189 private:
190 class TreeviewRootChangeAction : public ComponentUndoableAction <TreeView>
192 public:
193 TreeviewRootChangeAction (TreeView* const comp, ComponentLayout& layout, const bool newState_)
194 : ComponentUndoableAction <TreeView> (comp, layout),
195 newState (newState_)
197 oldState = comp->isRootItemVisible();
200 bool perform()
202 showCorrectTab();
203 getComponent()->setRootItemVisible (newState);
204 changed();
205 return true;
208 bool undo()
210 showCorrectTab();
211 getComponent()->setRootItemVisible (oldState);
212 changed();
213 return true;
216 bool newState, oldState;
220 //==============================================================================
221 class TreeViewRootOpennessProperty : public ComponentChoiceProperty <TreeView>
223 public:
224 TreeViewRootOpennessProperty (TreeView* comp, JucerDocument& document)
225 : ComponentChoiceProperty <TreeView> ("default openness", comp, document)
227 choices.add ("Items open by default");
228 choices.add ("Items closed by default");
231 void setIndex (int newIndex)
233 document.perform (new TreeviewOpennessChangeAction (component, *document.getComponentLayout(), newIndex == 0),
234 "Change TreeView openness");
237 int getIndex() const
239 return component->areItemsOpenByDefault() ? 0 : 1;
242 private:
243 class TreeviewOpennessChangeAction : public ComponentUndoableAction <TreeView>
245 public:
246 TreeviewOpennessChangeAction (TreeView* const comp, ComponentLayout& layout, const bool newState_)
247 : ComponentUndoableAction <TreeView> (comp, layout),
248 newState (newState_)
250 oldState = comp->areItemsOpenByDefault();
253 bool perform()
255 showCorrectTab();
256 getComponent()->setDefaultOpenness (newState);
257 changed();
258 return true;
261 bool undo()
263 showCorrectTab();
264 getComponent()->setDefaultOpenness (oldState);
265 changed();
266 return true;
269 bool newState, oldState;
275 #endif // __JUCER_TREEVIEWHANDLER_JUCEHEADER__